package divinity.objects
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import divinity.pathfinding.*;
	import divinity.pathfinding.PathNode;
	import divinity.utils.Calculator;
	import divinity.utils.Filters;
	
	public class ControllableObject extends ActiveObject
	{
		private var _myHeight:Number;
		private var _fac:Number=10/1000;
		private var _moving:Boolean=false;
		
		//
		private var _moveLeft:Boolean=false;
		private var _moveRight:Boolean=false;
		private var _moveUp:Boolean=false;
		private var _moveDown:Boolean=false;
		private var _moveDestination:Point;
		private var _calculator:Calculator=new Calculator();
		private var _path:Array;
		public function ControllableObject(wld:*)
		{					
			super();
			world=wld;	
			speed=80;
			speedFactor=1;
			
			
			
		}
		override public function init(e:Event)
		{
			
			super.init(e);
			_myHeight=world.getHeight(x,y);
			_moveDestination=new Point(x,y);
			
		}
		override protected function setShadow()
		{
			//var dist:Number=world.getHeight(x,y);			
			//Filters.addFilters(img,"DropShadowFilter,quality:3,angle:"+world.sunAngle+",distance:10,alpha:"+world.sunStrength);
		}
		override protected function moving():void
		{
			super.moving();
			checkColission()
			var movable=world.getMovable(_newx,_newy);
			trace(movable);
			if(!movable)
			{
				_g=0;
				_w=0;
			}
			
		}
		private function checkColission()
		{
			var checkX:Number;
			var checkY:Number;
			for(var n=0;n<_elapsed;n++)
			{
				trace(n,moveMechancis(n));
			}
		}
		public function moveDestionation(dx,dy)
		{
			var movable:Boolean=world.getMovable(dx,dy);
			if(movable)
			{
				trace("SETTING DESTINATION");
				_moveDestination=new Point(dx,dy);
				findPath(x,y,dx,dy);
			}
			else
			{
				trace("cant move there");
			}
		}
		
		
		private function findPath(sx,sy,dx,dy)
		{
			_path=[];
			var pathDone:Boolean=false;
			var v:Object=_calculator.point2point(this,_moveDestination);			
			
			var n=0;		
					
			while(!pathDone)
			{
				var test=new List({x:sx,y:sy},{x:dx,y:dy},world);
				_path.push(test.best)
				sx=test.best.checked.x;
				sy=test.best.checked.y;
				if(n==1000)
				{
					pathDone=true;
				}
				n++;
				
				
			}
			//drawPath()
			
			
			
		}
		
		private function drawPath()
		{
			trace("drawing path on ",parent);
			var d=new Sprite();
			
			world.OBJECTS.addChild(d);
			d.graphics.lineStyle(5,0xff0000,.5);
			d.graphics.moveTo(x,y);
			for(var n=0;n<_path.length;n+=1)
			{
				//trace(_path[n].movable);
				d.graphics.lineTo(_path[n].checked.x,_path[n].checked.y);
			}
		}
		
		
		public function get myHeight()
		{
			return _myHeight;
		}
		/**
		 * 
		 * 
		 * 
		 * */
		public function set moveRight(value:Boolean)
		{
			_moveRight=value;
		}
		public function set moveLeft(value:Boolean)
		{
			_moveLeft=value;	
		}
		public function set moveUp(value:Boolean)
		{
			_moveUp=value;	
		}
		public function set moveDown(value:Boolean)
		{
			_moveDown=value;	
		}
		
		
	}
}